package com.example.shixi.service;

import com.example.shixi.domain.BlogFriendAddress;
import com.example.shixi.domain.BlogFriendAddressExample;
import com.example.shixi.mapper.BlogFriendAddressMapper;
import com.example.shixi.pojo.UserPage;
import com.example.shixi.util.JSONResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class FriendService {
    @Resource
    private BlogFriendAddressMapper blogFriendAddressMapper;
    public JSONResult query(BlogFriendAddress articleName, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        BlogFriendAddressExample blogFriendAddressExample = new BlogFriendAddressExample();
        BlogFriendAddressExample.Criteria criteria = blogFriendAddressExample.createCriteria();
        //BlogFriendAddressExample blogFriendAddressExampleAll = new BlogFriendAddressExample();
        UserPage<BlogFriendAddress> userPage= new UserPage<>();

        //List<BlogFriendAddress> blogFriendAddresssAll = blogFriendAddressMapper.selectByExample(blogFriendAddressExampleAll);
        if(!ObjectUtils.isEmpty(articleName)){
            criteria.andNameLike("%"+articleName.getName()+"%");
        }

        List<BlogFriendAddress> blogFriendAddresss = blogFriendAddressMapper.selectByExample(blogFriendAddressExample);
        PageInfo<BlogFriendAddress> pageInfo= new PageInfo<>(blogFriendAddresss);
        userPage.setList(blogFriendAddresss);
        userPage.setTotal(pageInfo.getTotal());
        userPage.setPageNum(pageNum);
        userPage.setPageSize(pageSize);
        return JSONResult.success(userPage);

    }

    public JSONResult add(BlogFriendAddress ba) {
        blogFriendAddressMapper.insertSelective(ba);
        return JSONResult.success("成功");
    }

    public JSONResult del(Long id) {
        blogFriendAddressMapper.deleteByPrimaryKey(id);
        return JSONResult.success("成功");
    }

    public JSONResult update(BlogFriendAddress ba) {
        blogFriendAddressMapper.updateByPrimaryKeySelective(ba);
        return JSONResult.success("成功");
    }
}
